package view.admin;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import model.GraphAPI;

import controller.MovementHandler;
import controller.ActionEvents.ActionHandler;
import javax.swing.JComboBox;
import javax.swing.JList;
import java.awt.Button;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;

public class AdminStatistics extends JPanel {

private ActionHandler actionHandler;
private JButton btnMainMenu;
private JLabel lblStatistics;
private MovementHandler movementHandler;
private double [][] data;
int mouseX, mouseY;
private JList listUsers;
private JButton btnSubmitStatisticsChange;
private JRadioButton rdbtnSpecificUsersAccount;
private JRadioButton rdbtnUsersPasswordLength;
private JRadioButton rdbtnAllAccountBalances;
private JRadioButton rdbtnYourOwnTransactions;
private final ButtonGroup buttonGroup = new ButtonGroup();
	/**
	 * Create the panel.
	 */
	public AdminStatistics(ActionHandler actionHandler) {
		setPreferredSize(new Dimension(750, 480));
		setLayout(null);
		setBackground(Color.WHITE);
		
		this.actionHandler = actionHandler;
		
		movementHandler = new MovementHandler(this);
		addMouseMotionListener(movementHandler);
		
		
	lblStatistics = new JLabel("Statistics");
	lblStatistics.setFont(new Font("Lucida Grande", Font.PLAIN, 28));
	lblStatistics.setBounds(327, 24, 130, 36);
	add(lblStatistics);	
	
		// Button returns to admin main menu
		btnMainMenu = new JButton("Main menu");
		btnMainMenu.setBounds(677, 451, 117, 29);
		btnMainMenu.addActionListener(actionHandler);
		btnMainMenu.setBorderPainted(false);
		btnMainMenu.setContentAreaFilled(false);
		btnMainMenu.setActionCommand("backToAdminMenu");
		add(btnMainMenu);
		
		btnSubmitStatisticsChange = new JButton("");
		btnSubmitStatisticsChange.setIcon(new ImageIcon(AdminStatistics.class.getResource("/Pictures/green/submit.png")));
		btnSubmitStatisticsChange.setBounds(598, 112, 134, 39);
		btnSubmitStatisticsChange.setBorderPainted(false);
		btnSubmitStatisticsChange.setContentAreaFilled(false);
		btnSubmitStatisticsChange.addActionListener(actionHandler);
		btnSubmitStatisticsChange.setActionCommand("submitAdminStatisticsChange");
		add(btnSubmitStatisticsChange);
		
		// JList which contains the users
		listUsers = new JList();
		listUsers.setBackground(Color.WHITE);
		
		// Ad a JScrollPane to JList
		JScrollPane scrollPane = new JScrollPane(listUsers);
		scrollPane.setBounds(459, 6, 113, 106);
		scrollPane.setBackground(Color.GRAY);
		add(scrollPane);
		
		rdbtnSpecificUsersAccount = new JRadioButton("An users account balance");
		buttonGroup.add(rdbtnSpecificUsersAccount);
		rdbtnSpecificUsersAccount.setBounds(572, 6, 222, 32);
		rdbtnSpecificUsersAccount.addActionListener(actionHandler);
		rdbtnSpecificUsersAccount.setActionCommand("setGraphSpecificUsersAccount");
		add(rdbtnSpecificUsersAccount);
		
		rdbtnUsersPasswordLength = new JRadioButton("Users password length");
		buttonGroup.add(rdbtnUsersPasswordLength);
		rdbtnUsersPasswordLength.setBounds(572, 31, 187, 32);
		rdbtnUsersPasswordLength.addActionListener(actionHandler);
		rdbtnUsersPasswordLength.setActionCommand("setGraphUsersPasswordLength");
		add(rdbtnUsersPasswordLength);
		
		rdbtnAllAccountBalances = new JRadioButton("All account balances");
		buttonGroup.add(rdbtnAllAccountBalances);
		rdbtnAllAccountBalances.setBounds(572, 55, 174, 32);
		rdbtnAllAccountBalances.addActionListener(actionHandler);
		rdbtnAllAccountBalances.setActionCommand("setGraphAllAccountBalances");
		add(rdbtnAllAccountBalances);
		
		rdbtnYourOwnTransactions = new JRadioButton("Your own transactions");
		buttonGroup.add(rdbtnYourOwnTransactions);
		rdbtnYourOwnTransactions.setBounds(572, 78, 187, 32);
		rdbtnYourOwnTransactions.addActionListener(actionHandler);
		rdbtnYourOwnTransactions.setActionCommand("setGraphYourOwnTransactions");
		add(rdbtnYourOwnTransactions);
		
//		Random r = new Random();
		
//		data = new double [40][1];
//		for(int j = 0 ; j<1;j++){
//			for(int i = 0;i<40;i++){
//				data[i][j]=r.nextInt(40);
//			}
//		}
	}
	/**
	 * @return the btnMainMenu
	 */
	public JButton getBtnMainMenu() {
		return btnMainMenu;
	}
	/**
	 * @return the btnSubmitStatisticsChange
	 */
	public JButton getBtnSubmitStatisticsChange() {
		return btnSubmitStatisticsChange;
	}
	/**
	 * @return the rdbtnSpecificUsersAccount
	 */
	public JRadioButton getRdbtnSpecificUsersAccount() {
		return rdbtnSpecificUsersAccount;
	}
	/**
	 * @return the rdbtnUsersPasswordLength
	 */
	public JRadioButton getRdbtnUsersPasswordLength() {
		return rdbtnUsersPasswordLength;
	}
	/**
	 * @return the rdbtnAllAccountBalances
	 */
	public JRadioButton getRdbtnAllAccountBalances() {
		return rdbtnAllAccountBalances;
	}
	/**
	 * @return the rdbtnYourOwnTransactions
	 */
	public JRadioButton getRdbtnYourOwnTransactions() {
		return rdbtnYourOwnTransactions;
	}
	/**
	 * paintComponents uses the Java Graphics API to draw a graph on the panel. This is done by calling
	 * paintDataGraph with the graph bounds and data model as an argument.
	 */
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		GraphAPI.paintBarGraph(g2,data, 30, 135, 580, 300); //Invokes method which sets the object g2
		GraphAPI.paintLinesMouse(g2,data, 30, 135, 580, 300, mouseX, mouseY, 1);
	}

	/**
	 * @return the mouseX
	 */
	public int getMouseX() {
		return mouseX;
	}

	/**
	 * @param mouseX the mouseX to set
	 */
	public void setMouseX(int mouseX) {
		this.mouseX = mouseX;
	}

	/**
	 * @return the mouseY
	 */
	public int getMouseY() {
		return mouseY;
	}

	/**
	 * @param mouseY the mouseY to set
	 */
	public void setMouseY(int mouseY) {
		this.mouseY = mouseY;
	}

	/**
	 * @return the data
	 */
	public double[][] getData() {
		return data;
	}

	/**
	 * @param data the data to set
	 */
	public void setData(double[][] data) {
		this.data = data;
	}

	/**
	 * @return the listUsers
	 */
	public JList getListUsers() {
		return listUsers;
	}
}
